import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // 启用全局验证管道
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
      transform: true,
      transformOptions: {
        enableImplicitConversion: true,
      },
    }),
  );

  // 启用 CORS
  app.enableCors({
    origin: '*',
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
    credentials: true,
  });

  // Swagger 配置
  const config = new DocumentBuilder()
    .setTitle('Todo List API')
    .setDescription('Todo List 应用的 API 接口文档')
    .setVersion('1.0')
    .addTag('auth', '认证相关接口')
    .addTag('todos', '任务相关接口')
    .addTag('users', '用户相关接口')
    .addTag('teams', '团队相关接口')
    .addBearerAuth(
      {
        type: 'http',
        scheme: 'bearer',
        bearerFormat: 'JWT',
        name: 'JWT',
        description: '输入 JWT token',
        in: 'header',
      },
      'JWT-auth', // 这个名字将在需要认证的接口中使用
    )
    .build();

  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api-docs', app, document, {
    swaggerOptions: {
      persistAuthorization: true, // 保持授权状态
    },
  });

  await app.listen(3001);
  console.log('Server is running on http://localhost:3001');
  console.log('Swagger API Docs: http://localhost:3001/api-docs');
}
bootstrap();
